package game.UIComponent {
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	import flash.text.TextFormat;
	
	import game.common.CustomBitmap;
	import game.common.Reflection;
	import game.utils.ColorConstant;
	import game.utils.Utils;
	
	public class TabButton extends UIBase {
		
		private var background:CustomBitmap= new CustomBitmap();
		private var label:TextField = Utils.getTextField("",12);
		private var link:Sprite;
		private var isChoose:Boolean;
		private var _click:Function;
		private var skin:String;
		private var _index:int;
		private var _clickFun:Function;
		
		public function TabButton(title:String, clickEvent:Function, _skin:String="tabBtnbg") {
			
			skin = _skin;
			_clickFun=clickEvent;
			
			this.buttonMode = true;
			background.source = skin + "_up";
			background.mouseChildren = false;
			background.mouseEnabled = false;
			addChild(background);
			label.textColor = ColorConstant.getColorInt(32);
			label.text = title;
			label.autoSize=TextFieldAutoSize.LEFT;
			label.width = label.textWidth ;//+ 4;
			label.height = label.textHeight + 4;
			label.x = (width - label.width )/2;
			label.y = (height - label.height)/2; 
			addChild(label);
			
			
			addEvent();		
		}
		
		private function onMouseOver(e:MouseEvent):void
		{
			background.source = skin + "_over";
		}
		
		private function onMouseOut(e:MouseEvent):void
		{
			background.source = skin + "_up";
		}
		
		public function set binding(sprite:Sprite):void {
			link = sprite;
		}
		public function get binding():Sprite {
			return link;
		}
		
		public function set text(value:String):void {
			label.text = value;
			label.x = (background.width - label.width)/2;
		}
		
		public override function set width(value:Number):void {
			background.width = value;
		}
		
		public function getLink():Object {
			return link;
		}
		
		public function set choose(value:Boolean):void {
			if(value) {
				background.source = skin + "_over";
				removeEvent();				
			}else {
				background.source = skin+"_up";
				addEvent();
			}
			
			if(link){
				link.visible = value;
			}

			isChoose = value;
		}
		
		private function removeEvent():void{
			removeEventListener(MouseEvent.MOUSE_OUT,onMouseOut);
			removeEventListener(MouseEvent.MOUSE_OVER,onMouseOver);
			removeEventListener(MouseEvent.CLICK,_clickFun);
		
			mouseEnabled=false;
			buttonMode=false;
		}
		
		private function addEvent():void{
			addEventListener(MouseEvent.MOUSE_OVER, onMouseOver);
			addEventListener(MouseEvent.MOUSE_OUT, onMouseOut);
			addEventListener(MouseEvent.CLICK, _clickFun);
			
			mouseEnabled=true;
			buttonMode=true;
			
		}
	}
}
